#!/bin/bash

#	 			Copyright 2010 Morris Blackham
#
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


#   This is the install script script to check dependencies and copy file
#   to the correct location.
#   **  More to come **
#


# set some variables and create directory structures

scriptdir="/opt/gwack"
configdir="/etc/opt/gwack"
webadmindir="/srv/www/htdocs/gwack"
dbcopy="/opt/novell/groupwise/agents/bin/dbcopy"
gwpoa="/opt/novell/groupwise/agents/bin/gwpoa"

dirs=($scriptdir $configdir $webadmindir)

for dir in ${dirs[*]}
	do
	if [ ! -d $dir ]; then
		mkdir -p $dir
		echo "Creating directory $dir..."
		fi
done

cp ./scripts/* $scriptdir
echo "Copying files to $scriptdir"
cp -r ./webadmin/* $webadmindir
echo "Copying files to $webadmindir"

#  unremark this when gwack.xml is finalized
cp -r ./config/* $configdir
echo "Copying files to $configdir"

#check for python and php

package=(python php)

for y in ${package[*]}

do
	#echo $y
	which $y&>/dev/null
	retval=$?

	[ $retval == 0 ] && echo "$y found.  Continuing"

	[ $retval != 0 ] && echo "$y not found.  Please install before continuing"

done

#check for apache

apache_link=/usr/sbin/httpd2
test -L $apache_link && apache_bin=$(readlink $apache_link)
retval=$?

[ $retval == 0 ] && echo "Apache2 found."
[ $retval != 0 ] && echo "Apache2 not found.  Please install Apache before continuing"


#check for dbcopy and gwagents

if [ ! -e $dbcopy ]; then
	echo "Dbcopy not found.  Please install from GroupWise Linux Distribution."
else
	echo "Dbcopy found"

fi

if [ ! -e $gwpoa ]; then
	echo "GroupWise agents not found.  Please install from GroupWise Linux Distribution."
	echo "agents should be installed to enable loading agents against backup"
fi

CRON_FILE=/etc/cron.d/gwack

function createcron {

   echo "Please enter time to start script.  Enter as hh:mm in 24hr format."
   read starttime

   declare -a times
   declare -i hour
   declare -i min
   times=(`echo $starttime | tr ":" " "`)
   hour=${times[0]}
   min=${times[1]}

   if [[ $hour -gt 25 ]]; then
      echo "hour value not valid;  enter a correct hour"
   fi
   if [[ $min -gt 60 ]]; then
      echo "minute value not valid,  enter valid minutes"
   fi

   echo "# gwack cron file" > $CRON_FILE
   echo "$min $hour * * * root /opt/gwack/gwack.py >> /dev/null 2>&1" >> $CRON_FILE;

}

echo "Create cron entry to load Gwack?"
select CRON in "Yes" "No"; do

   case $CRON in

      Yes )

         if [ -f $CRON_FILE ]; then
            echo "gwack cron file exists, do you want to edit values? y/n" yn
            select yn in "Yes" "No"; do
            case $yn in
               Yes )
                  createcron;;

               No ) 
				  break;;

            esac
         done

         else
            createcron
         fi;
         break;;

      No ) break;;
   esac
done


#  for future, need to find better way to create conf file for apache..
#
#echo "Do you want to setup Apache to use authentication for Gwack?"
#select AUTH in "Yes" "No"; do

#	case $AUTH in
#		Yes )
#			if [ -f "./SetupAuth" ]; then
#				python SetupAuth
#				break
#			else
#				echo "SetupAuth script not found"
#			fi
#			;;
#		No ) break;;
#	esac
#done

echo ""
echo "The gwack.xml config file needs to be write enabled for the user"
echo "that runs web services.  This is normally the wwwrun user."
echo "If you are running Apache as a different user,  you must manually"
echo "set proper permissions for that user for the /etc/opt/gwack directory"
echo "and the /etc/opt/gwack/gwack.xml file by changing ownership to the"
echo "proper user."
echo ""
echo "Set owner to wwwrun user?"

select YN in "Yes" "No"; do

    case $YN in
        Yes )
			chown -R wwwrun /etc/opt/gwack
           break  ;;
        No ) exit;;
    esac
done

